﻿// -----------------------------------------------------------------------
// <copyright file="Helper.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Core.Player
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Xml;
    using Core.Config;

    /// <summary>
    /// Contains useful methods
    /// </summary>
    public static class Helper
    {
        /// <summary>Used to load a xml from file where our filters are stored</summary>
        private static XmlDocument xmlDocument = new XmlDocument();

        /// <summary>Stores all audio extensions concatenated</summary>
        private static string filterAudio;

        /// <summary>Stores all video extensions concatenated</summary>
        private static string filterVideo;

        /// <summary>Stores supported file extensions</summary>
        private static FileExtensions fileExtensions;

        /// <summary>
        /// File types
        /// </summary>
        public enum FileTypeEnum
        {
            /// <summary>Playing local audio file</summary>
            LocalAudio,

            /// <summary>Playing local video file</summary>
            LocalVideo,

            /// <summary>Playing internet audio streaming</summary>
            InternetAudioStream,

            /// <summary>Playing internet video streaming</summary>
            InternetVideoStream,

            /// <summary>Unknown file type</summary>
            Unknown
        }

        /// <summary>
        /// Check the type of the file
        /// </summary>
        /// <param name="file">Specify the file name</param>
        /// <returns>The type of the file</returns>
        public static FileTypeEnum CheckFileType(string file)
        {
            if (filterAudio == null)
            {
                LoadFileExtensions();
            }

            if (file == null)
            {
                return FileTypeEnum.Unknown;
            }

            // Check if is radio (This is not good, should be improved!)
            if (file.Contains("http://"))
            {
                return FileTypeEnum.InternetAudioStream;
            }

            string fileExtension = GetFileExtension(file);

            switch (fileExtension)
            {
                // Radio
                case ".pls":
                    return FileTypeEnum.InternetAudioStream;
                case ".m3u":
                    return FileTypeEnum.InternetAudioStream;
            }

            // If it's not radio, maybe is local audio or video :P
            bool inListAudio = filterAudio.Contains(fileExtension);
            bool inListVideo = filterVideo.Contains(fileExtension);

            // Return format type: Audio / Video
            if (inListAudio == true)
            {
                return FileTypeEnum.LocalAudio;
            }
            else if (inListVideo == true)
            {
                return FileTypeEnum.LocalVideo;
            }

            // We don't know what is this...
            return FileTypeEnum.Unknown;
        }

        /// <summary>
        /// Get extension from path
        /// </summary>
        /// <param name="file">Full file path</param>
        /// <returns>The extension of the file</returns>
        public static string GetFileExtension(string file)
        {
            return Path.GetExtension(file);
        }

        /// <summary>
        /// Check specific extension
        /// </summary>
        /// <param name="extenstion">The extension to check</param>
        /// <returns>Returns true if the file type is supported</returns>
        public static bool IsSupported(string extenstion)
        {
            if (fileExtensions == null)
            {
                LoadFileExtensions();
            }

            return fileExtensions.IsSupported(extenstion);
        }

        /// <summary>
        /// Initialize and load supported extensions from xml
        /// </summary>
        private static void LoadFileExtensions()
        {
            fileExtensions = new FileExtensions();

            // This XML contains all the supported audio and video extension files.
            xmlDocument.Load(Paths.UserFolder + "\\FileExtensions.xml");

            // Build filters - Load the supported extensions into a string (FilterAudio and FilterVideo)

            // Get all audio extensions
            try
            {
                int nrItems;
                nrItems = fileExtensions.GetAllExtensions(FileExtensions.DataType.audio);
                if (nrItems > 0)
                {
                    foreach (XmlNode node in fileExtensions.XmlExtensions)
                    {
                        string extension = node.FirstChild.Name.ToString();
                        string ext2 = extension.Replace("xx", string.Empty);

                        filterAudio = filterAudio + " ." + ext2;
                    }
                }
            }
            catch
            {
            }

            // Get all video extensions
            try
            {
                int nrItems;
                nrItems = fileExtensions.GetAllExtensions(FileExtensions.DataType.video);
                if (nrItems > 0)
                {
                    foreach (XmlNode node in fileExtensions.XmlExtensions)
                    {
                        string extension = node.FirstChild.Name.ToString();
                        string ext2 = extension.Replace("xx", string.Empty);

                        filterVideo = filterVideo + " ." + ext2;
                    }
                }
            }
            catch
            {
            }
        }
    }
}
